<?php
namespace app\index\controller;

use app\index\model\MemberCoupon as MemberCouponModel;
use think\Request;
use think\Exception;
use think\Db;
class MemberCoupon
{

    //会员优惠券和 领取优惠券列表status还是不一样的
    /*
     * 用户端 - 我的优惠券列表
     * @param  string $user_id      会员ID
     * @param  string $status       使用状态
     * @param  string $end_time     已过期
     */
    public function index($member_id,$status,$type = 0,$single_id = 0,$order_price = 10000)
    {

        if(empty($order_price)) $order_price = 10000;
        // 获取参数	1是未使用 未过期2已使用  3是已过期
        //$data = Request::instance()->param();
        $time = time();
        $where = $whereor1 = $whereor2 = $whereor =  [];
        $where['auding_status'] = ['eq',2];
        if(!empty($status) && $status == 1){
            $where['status'] = ['eq',$status];
            $where['start_time'] = ['elt',$time];
            $where['end_time'] = ['gt',$time];
        }else if(!empty($status) && $status == 2) {
            $where['status'] = ['eq', $status];
        }else if(!empty($status) && $status == 3) {
            $condition['status'] = ['eq',1];
            $whereor = 'start_time > '.time().' or end_time <'.time();
        }else{
            return ['code' => 400, 'message' => 'error'];
        }
        if($type == 1){//商品
            $where['type'] = ['eq',1];
        }elseif($type == 2){
            $where['type'] = ['eq',2];
            $where['single_id'] = ['eq',$single_id];
        }
         $model = new MemberCouponModel();
         $result = $model->where($where)
            ->field('id,type,single_id,title,minus_price,total_price,content,status,start_time,end_time')
             ->where($whereor)
             ->where('total_price','elt',$order_price)
            ->order(['id' => 'desc'])
            ->fetchSql(false)
            ->where('member_id',$member_id)
            ->paginate(6)->each(function($item,$key){
                $item->start_time = date('Y-m-d',$item->start_time);
                 $item->end_time = date('Y-m-d',$item->end_time);
             });
        $on_off = Db::name('config')->where('varname','web_coupon_is_give')->value('varvalue');
        return ['code' => 200, 'message' => '成功', 'data' => $result,'on_off' => $on_off ];
    }

    public function on_off($member_id){

        $on_off = Db::name('config')->where('varname','web_coupon_is_give')->value('varvalue');
    }

    public function count($member_id,$status = 0,$type = 0,$single_id = 0,$order_price = 10000)
    {
        try {
            if (empty($order_price)) $order_price = 10000;
            // 获取参数	1是未使用 未过期2已使用  3是已过期
            //$data = Request::instance()->param();
            $time = time();
            $where = $whereor1 = $whereor2 = $whereor = [];

            $model = new MemberCouponModel();
            $where1['status'] = ['eq', 1];
            $where1['start_time'] = ['elt', $time];
            $where1['end_time'] = ['gt', $time];
            $where1['auding_status'] = ['eq',2];
            $count_1 = $model->where($where1)
                ->field('id,type,single_id,title,minus_price,total_price,content,status,start_time,end_time')
                ->where('total_price', 'elt', $order_price)
                ->order(['id' => 'desc'])
                ->fetchSql(false)
                ->where('member_id', $member_id)
                ->count();
            $where2['auding_status'] = ['eq',2];
            $where2['start_time'] = ['elt', $time];
            $where2['end_time'] = ['gt', $time];
            $where2['status'] = ['eq', 2];
            $count_2 = $model->where($where2)
                ->field('id,type,single_id,title,minus_price,total_price,content,status,start_time,end_time')
                ->where('total_price', 'elt', $order_price)
                ->order(['id' => 'desc'])
                ->fetchSql(false)
                ->where('member_id', $member_id)
                ->count();
            $whereor = 'start_time > ' . time() . ' or end_time <' . time();
            $count_3 = $model
                ->field('id,type,single_id,title,minus_price,total_price,content,status,start_time,end_time')
                ->where($whereor)
                ->where('total_price', 'elt', $order_price)
                ->order(['id' => 'desc'])
                ->fetchSql(false)
                ->where('member_id', $member_id)
                ->count();
            return ['code' => 200, 'message' => '优惠券各个状态数量', 'status_1' => $count_1, 'status_2' => $count_2, 'status3' => $count_3];
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }


    /**
     * @param $id
     * @param $member_id
     * @param $username
     * @return array 转增会员优惠券
     */
    public function give($id, $member_id, $username)
    {
        $member_coupon = Db::name('member_coupon')->where('id', $id)->where('member_id', $member_id)->find();
        if (empty($member_coupon)) return ['code' => 400, 'message' => '当前会员优惠券不存在'];
        $member = Db::name('member')->where('username', $username)->find();
        if (empty($member)) return ['code' => 400, 'message' => '要转增的会员不存在'];
        $ret = Db::name('member_coupon')->where('id', $id)->where('member_id', $member_id)->setField('member_id', $member['id']);
        if ($ret !== false) {
            return ['code' => 200, 'message' => '转赠成功'];
        }
        return ['code' => 400, 'message' => '失败'];
    }

}
